<?php

class Blog_ComentariosController extends Zend_Controller_Action
{

    protected $_comentarios = null;

    public function init()
    {
        /* Initialize action controller here */
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->cliente = Zend_Auth::getInstance()->getIdentity();
        $this->_comentarios = new Blog_Model_DbTable_Comentarios();
    }

    public function indexAction()
    {
        // action body
        $this->_redirect('/blog');
    }

    public function crearAction()
    {
        //http://danielbonhaure/blog/comentarios/crear/articulo/#
        $id_art = $this->_getParam('articulo');
        $id_cliente = Zend_Auth::getInstance()->getIdentity()->id;
        
        $form = new Blog_Form_Comentario();
        $form->setAction('/blog/comentarios/crear/articulo/'.$id_art);
        $form->setMethod('post');
        
        if( $this->getRequest()->isPost() ){
            if( $form->isValid( $this->_getAllParams() ) ) {
                $texto = $form->getValue('texto');
                $this->_comentarios->crearComentario($texto, $id_art, $id_cliente);
                $this->_redirect('/blog/articulos/ver/id/'.$id_art);
            }      
            $this->view->form = $form;    
        }elseif( $this->getRequest()->isGet() ){
            $this->view->form = $form;
        }
        
    }

    public function borrarAction()
    {
        //http://danielbonhaure/blog/comentarios/borrar/id/#
        $id = $this->_getParam('id');
        $coment = $this->_comentarios->obtenerComentario($id);
        $this->_comentarios->borrarComentario($id);
        $this->_redirect('/blog/articulos/ver/id/'.$coment->findParentRow('Blog_Model_DbTable_Articulos', 'Articulo')->id);
    }


}





